package com.wdh.kjs.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.R;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.http.response.MyCookieStore;
import com.wdh.kjs.model.Auth_per;
import com.wdh.kjs.model.JsonModel;
import com.wdh.kjs.model.JsonModel_my;
import com.wdh.kjs.util.ToastUtils;

import org.apache.http.impl.client.DefaultHttpClient;

import java.math.BigDecimal;

/**
 * Created by CQH on 2016/4/21.
 */
public class RealNameAuthPerActivity extends BaseTopActivity implements View.OnClickListener{
    private Intent intent;
    private Button realName_per_submit;
    private EditText EditText_auth_per_pName;
    private EditText EditText_auth_per_pIdNum;
    private String partyId;
    private String url="";//第一次实名认证或退回后重新认证
    private String auditingOpinion="0";
    private JsonModel jsonModel;
    private String zhanghu;
    private String mima;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.real_name_auth_per);
        SharedPreferences sharedPreferences = getSharedPreferences("logininfo",MODE_PRIVATE);
        partyId = sharedPreferences.getString("loginToken", "");
        zhanghu = sharedPreferences.getString("zhanghao", "");
        mima = sharedPreferences.getString("mima", "");
        initTopBar("实名认证");
        initshiming();
        Intent intent1=getIntent();
        url=intent1.getStringExtra("URL");
        //System.out.println("个人认证:"+url);
        if(Httpurl.URL_REALAUTH_PER_BACK.equals(url)){
            denglu();
        }
        initview();
    }
    private void initshiming() {
        HttpUtils http = new HttpUtils();
        RequestParams params = new RequestParams();

//            params.addBodyParameter("emailAddress","742051747@qq.com" );
        params.addBodyParameter("partyId", partyId);

        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_HUOQURENZHENG, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {


                Gson gson = new Gson();
                jsonModel = gson.fromJson(responseInfo.result, JsonModel.class);
                if (jsonModel.success)
                    auditingOpinion = jsonModel.getRealAuthInfo().getAuditingOpinion();
            }

            @Override
            public void onFailure(HttpException error, String msg) {


            }
        });

    }
    private void loadData() {
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("partyId", partyId);
        params.addQueryStringParameter("pName", EditText_auth_per_pName.getText().toString());
        params.addQueryStringParameter("pIdNum",EditText_auth_per_pIdNum.getText().toString());

        HttpUtils http = new HttpUtils();
        if(Httpurl.URL_REALAUTH_PER_BACK.equals(url)){
            http.configCookieStore(MyCookieStore.firstCookieStore);
        }
        http.send(HttpRequest.HttpMethod.POST,url, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Gson gson = new Gson();
                Auth_per auth_per = gson.fromJson(responseInfo.result, Auth_per.class);

                if (auth_per.isSuccess()) {
                    trueDialogShow("实名认证成功!");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            intent = new Intent();
                            SharedPreferences.Editor editor = getSharedPreferences("logininfo",MODE_PRIVATE).edit();
                            editor.putString("type","p");
                            if (!auditingOpinion.equals("0")) {
                                editor.putString("levels", "1");
                            }
                            editor.putString("nickName", EditText_auth_per_pName.getText().toString());
                            editor.commit();
                            finish();
                            intent.setClass(RealNameAuthPerActivity.this, SecuritySetting.class);
//                            intent.putExtra("type","p");
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                        }
                    },2000);
                }else
                    ToastUtils.showShort(getApplicationContext(),"实名认证失败");
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                ToastUtils.showShort(getApplicationContext(), "网络异常！");
            }
        });

    }

    private void falseDialogShow(String hint) {
        View dialog = (LinearLayout) getLayoutInflater().inflate(R.layout.view_dialog_false, null);
        Button bt_dialog_false_negative = (Button) dialog.findViewById(R.id.bt_dialog_false_negative);
        Button bt_dialog_false_positive = (Button) dialog.findViewById(R.id.bt_dialog_false_positive);
        TextView tv_dialog_false_hint = (TextView) dialog.findViewById(R.id.tv_dialog_false_hint);
        tv_dialog_false_hint.setText(hint);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialog);
        builder.create();
        final AlertDialog dialogShow = builder.show();
        bt_dialog_false_negative.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogShow.dismiss();
            }
        });
        bt_dialog_false_positive.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogShow.dismiss();
                EditText_auth_per_pName.setText("");
                EditText_auth_per_pIdNum.setText("");
            }
        });
    }

    private void trueDialogShow(String hint) {
        View dialog = (LinearLayout) getLayoutInflater().inflate(R.layout.view_dialog_true, null);
        TextView tv_dialog_false_hint = (TextView) dialog.findViewById(R.id.tv_dialog_true_hint);
        tv_dialog_false_hint.setText(hint);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialog);
        builder.create();
        final AlertDialog dialogShow = builder.show();

    }

    private void initview() {
        realName_per_submit = (Button) findViewById(R.id.realName_per_submit);
        realName_per_submit.setOnClickListener(this);
        EditText_auth_per_pName = (EditText)findViewById(R.id.EditText_auth_per_pName);
        EditText_auth_per_pIdNum = (EditText)findViewById(R.id.EditText_auth_per_pIdNum);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.realName_per_submit:
                if(EditText_auth_per_pName.getText().toString().equals("")){
                    falseDialogShow("请输入姓名！");
                }else if(EditText_auth_per_pIdNum.getText().toString().equals("")){
                    falseDialogShow("请输入证件号码！");
                }else if ( EditText_auth_per_pName.getText().length()<2){
                    falseDialogShow("请输入正确的姓名！");
                }else
                    loadData();
                break;
        }
    }


    private void denglu() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("USERNAME", zhanghu);
        params.addBodyParameter("PASSWORD", mima);
        final HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_LOGIN, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Gson gson = new Gson();
                jsonModel = gson.fromJson(responseInfo.result, JsonModel.class);
                String m = jsonModel.getPartyId();
                if (jsonModel._LOGIN_PASSED_()) {
                    DefaultHttpClient zpf = (DefaultHttpClient) http.getHttpClient();
                    MyCookieStore.firstCookieStore = zpf.getCookieStore();
                    SharedPreferences.Editor editor = getSharedPreferences("logininfo", MODE_PRIVATE).edit();
                    editor.putString("loginToken", jsonModel.getPartyId());
                    editor.putString("nickName", jsonModel.getNickName());
                    editor.putString("phoneNumber", jsonModel.getPhoneNumber());
                    editor.putString("levels", jsonModel.getLevels());
                    editor.putString("type", jsonModel.getType());
                    editor.putString("zhanghao", zhanghu);
                    editor.putString("mima", mima);
                    editor.commit();
                }


            }

            @Override
            public void onFailure(HttpException error, String msg) {


            }
        });

    }
}